package hauwei.year2025.month4.day17;

import java.util.*;

public class CloudServer {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String MN=scanner.nextLine();
        String []MNs=MN.split(" ");
        int M=Integer.parseInt(MNs[0]);
        int N=Integer.parseInt(MNs[1]);
        List<String[]> map=new ArrayList<>();
        HashMap<String,String> parentMap=new HashMap<>();
        HashMap<String,Integer> netCount=new HashMap<>();
        for (int i=0;i<N;i++){
            String line=scanner.nextLine();
            String[] s = line.split(" ");
            String net=s[0];
            String parent=s[1];
            map.add(s);
            parentMap.put(net,parent);
        }
        scanner.close();

        parentMap.forEach((net,parent)->{
            System.out.println(net+" "+parent);
        });

        map.forEach(net->{
            int star=Integer.parseInt(net[2]);
            int number=Integer.parseInt(net[3]);

            String parent=net[0];

            //找到父节点,赋值给temNet
            while(parentMap.get(parent)!=null&&!parentMap.get(parent).equals("*")){
                parent=parentMap.get(parent);
            }

            int starNumber=0;

            if(star==1){//表示一般问题
                starNumber=2;
            }else{//严重问题
                starNumber=5;
            }

            int count=netCount.getOrDefault(parent,0);
            count+=starNumber*number;
            netCount.put(parent,count);//刷新

        });

        int sum=0;

        Set<String> strings = netCount.keySet();
        for(String s:strings){
            int count=netCount.get(s);
            if(count>=M){
                sum++;
            }
        }

        System.out.println(sum);
    }

}
